<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/7/28
 * Time: 17:40
 */

namespace Home\Service;


use Common\Common\Service;
use Home\Exception\HomeException;
use Home\Request\Weixin\GetWeixinRequest;
use Home\Component\WeixinComponent;
use Home\Response\GetWeixinResponse;

class GetWeixinService implements Service
{
    public function run(GetWeixinRequest $request = null)
    {
        if ($request->issetOpenId() == false)
            throw new HomeException(10001);
        $openid = $request->getOpenId();
        $component = new WeixinComponent();
        $response = new GetWeixinResponse();
        try {
            $result = $component->getWeixin($openid);
            if (is_null($result) == true) {
                $response->setItIsNull();
            } else {
                $nickname = $result['nickname'];
                $headimgurl = $result['headimgurl'];
                $response->setNickName($nickname);
                $response->setHeadImgUrl($headimgurl);
            }
        } catch (HomeException $e) {
            throw new HomeException($e->getMessage(), $e->getCode());
        }
        return $response;
    }
}